#include "zqueue.h"
#include <stdio.h>
#include <stdlib.h>

zqueue * zqueue_create()
{
	zqueue * pobj = (zqueue *)calloc(1, sizeof(zqueue));
	if (pobj == NULL)
	{
		return NULL;
	}
	return pobj;
}

int zqueue_delete(zqueue * pobj)
{
	if (NULL == pobj)
	{
		return -1;
	}

	free(pobj);
	return 0;
}

int zqueue_delete2(zqueue ** ppobj)
{
	zqueue * pobj = NULL;
	if (NULL == ppobj)
	{
		return -1;
	}
	pobj = *ppobj;
	if (NULL == pobj)
	{
		return -1;
	}
	free(*ppobj);
	*ppobj = NULL;
	return 0;
}

void * zquedeque(zqueue * pobj)
{
	void * ret = NULL;
	if (NULL == pobj)
	{
		return NULL;
	}
	ret = pobj->items[0];
	pobj->items++;
	return ret;
}

void * zqueenque(zqueue * pobj, void * item)
{
	void * reallocr = NULL;
	if (!pobj)
	{
		return NULL;
	}
	if (pobj->len >= pobj->opacity)
	{
		// realloc
		reallocr = realloc(pobj->items, (sizeof(void *)) * (pobj->len + 1)); 
		if (NULL == reallocr)
		{
			return NULL;
		}
		pobj->items = (void **)reallocr;
		pobj->opacity = pobj->len + 1;
	}
	pobj->items[pobj->len] = item;
	pobj->len++;
	return item;
}
